package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //创建一个新的对象
        Setmeal setmeal = new Setmeal();
        //把前端获取到的数据复制到这个新的对象里面
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        //从插入数据库里面的新的对象里面获取当前插入数据的ID
        Long setmealId= setmeal.getId();
        //从前端传入进来的数据拿到关联的菜品
        List<SetmealDish> setmealDishes =setmealDTO.getSetmealDishes();
        //循环这些菜品并且关联到前端传入的套餐里面
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //将这些菜品于套餐之间的关系插入到一个新的表里面
        setmealDishMapper.addSetmeal(setmealDishes);
    }

    @Override
    public PageResult pageQuerysetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page =setmealMapper.pageQuerysetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteIds(List<Long> ids) {
        ids.forEach(id->{
            Setmeal setmeal =setmealMapper.getByid(id);
            if (StatusConstant.ENABLE==setmeal.getStatus()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        ids.forEach(setmealId->{
            setmealMapper.deleteIds(setmealId);
            setmealDishMapper.deleteIds(setmealId);
        });
    }

    @Override
    public SetmealVO getSetmeal(Long id) {
        Setmeal setmeal=setmealMapper.getByid(id);
        List<SetmealDish> setmealDishes  = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteIds(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.addSetmeal(setmealDishes);
    }

    @Override
    //为了共用update那个函数的话因为里面的传入值是一个对象所以建立一个对象
    public void startOrstop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal>setmealList=setmealMapper.list(setmeal);
        return setmealList;
    }

    @Override
    public List<DishItemVO> getDishItemByid(Long id) {
        List<DishItemVO>dishItemVOList=setmealMapper.getDishItemByid(id);
        return dishItemVOList;
    }
}
